package com.powernode.sqlinjection;

import com.powernode.commons.util.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Arrebol
 * @Date 2024/8/1 23:52
 * @Project jdbc
 * @Description：
 * 使用PreparedStatement完成分页查询操作
 */
public class JdbcTestPreparedStatement5 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int pageSize = 3;
        int pageNumber = 2;
        try {
            conn= DbUtils.getConnection();
            String pageQuerySql="select * from t_emp limit ?,?";
            ps=conn.prepareStatement(pageQuerySql);
            ps.setInt(1,(pageNumber-1)*pageSize);
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            int count = 0;
            while (rs.next()){
                System.out.println(rs.getString("name"));
                count++;
            }
            System.out.println("查询到"+count+"条数据");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.close(rs,ps,conn);
        }
    }
}
